package myapp.baselibrary.http;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Objects;

/**
 * Created by yang2 on 2017/6/23.
 */

public class HttpUtils {

    private int mType = GET_TYPE;

    private static final int POST_TYPE = 0x0012;

    private static final int GET_TYPE = 0x0011;

    private String mUrl;
    private Context mContext;
    private Map<String,Object> mParams;

    private boolean mCache = false;

    public HttpUtils(Context context){
        this.mContext = context;
        mParams = new ArrayMap();
    }


    public static HttpUtils with(@NonNull Context context){

        return new HttpUtils(context);

    }

    public HttpUtils post(){
       this. mType = POST_TYPE;
        return this;
    }

    public HttpUtils get(){
        this. mType = GET_TYPE;
        return this;
    }
    public HttpUtils setUrl(String url){

        this.mUrl = url;
        return this;
    }
    public HttpUtils addParam(String key,Object value){
        mParams.put(key,value);
        return this;
    }
    public void execeute(EngineCallBack callBack){

        if (callBack==null){
            callBack = EngineCallBack.DEFUALT_CALL_BACK;
        }
        callBack.onPreExecute(mContext,mParams);
        if (mType  == POST_TYPE){
            post(mUrl,mParams,callBack);
        }
        if (mType == GET_TYPE){
            get(mUrl,mParams,callBack);
        }
    }
    public void execeute(){
        execeute(null);
    }

    public HttpUtils addParams(Map<String,Objects > params ){
        mParams.putAll(params);
        return this;
    }
    private static IHttpEngine mHttpEngine = null;


    public static void init(IHttpEngine httpEngine){
        mHttpEngine = httpEngine;
    }
    public void exchancgeEngine(IHttpEngine httpEngine){
        mHttpEngine = httpEngine;
    }


    private void get(String url, Map<String, Object> params, EngineCallBack callBack) {
        mHttpEngine.get(mCache,mContext,url,params,callBack);
    }

    private void post(String url, Map<String, Object> params, EngineCallBack callBack) {
        mHttpEngine.post(mCache,mContext,url,params,callBack);
    }


   public static Class<?> analysiscClazzInfo(Object o){
       Type getType = o.getClass().getGenericSuperclass();
       Type[] actualTypeArguments = ((ParameterizedType) getType).getActualTypeArguments();
       return (Class<?>) actualTypeArguments[0];
   }

    public static String jointParams(String url,Map<String ,Object> params){
        if (params!=null||params.size()<=0){
            return url;
        }
        StringBuffer stringBuffer = new StringBuffer(url);
        if (!url.contains("?")){
            stringBuffer.append("?");
        }else {
            if (!url.endsWith("&")){
                stringBuffer.append("&");
            }
        }
        for (Map.Entry<String,Object> entry:params.entrySet()){
            stringBuffer.append(entry.getKey()+"="+entry.getValue()+"&");
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);
        return stringBuffer.toString();
    }

    public HttpUtils cache(boolean isCache){
        this.mCache = isCache;
        return this;
    }

}
